package com.jxt.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jxt.app.dao.base.ApsharepostDAO;
import com.jxt.app.entity.base.Apsharepost;
import com.jxt.app.entity.base.ApsharepostExample;
import com.jxt.app.service.ApSharePostService;
import com.jxt.app.util.Page;

@Service
public class ApSharePostServiceImpl implements ApSharePostService{
	
	@Resource
	private ApsharepostDAO apsharepostDAO;

	@Override
	public int deleteModel(ApsharepostExample record) {
		// TODO Auto-generated method stub
		return apsharepostDAO.deleteByExample(record);
	}

	@Override
	public Page findListByExample(ApsharepostExample inparam, int page, int num)
			throws Exception {
		// TODO Auto-generated method stub
		Page result = new Page();

		try {
			/**
			 * 获取总记录条数
			 */
			int rowcount = apsharepostDAO.findCountByExample(inparam);
			/**
			 * 当没有符合查询条件的结果集时
			 */
			if (rowcount <= 0) {
				result.setCurPageNum(1);
				result.setPageCount(1);
				result.setResult(new ArrayList());
				result.setRowCount(0);
				result.setRowCountPerPage(num);
				return result;
			}
			/**
			 * 总页数
			 */
			long pagecount = 0;
			if (rowcount % num == 0) {
				pagecount = rowcount / num;
			} else {
				pagecount = rowcount / num + 1;
			}
			/**
			 * 根据总页数调整当前页码位置
			 */
			if (page <= 1) {
				page = 1;
			} else if (page > pagecount) {
				page = (int) pagecount;
				
				result.setCurPageNum(page);
				result.setPageCount(pagecount);
				result.setResult(new ArrayList());
				result.setRowCount(rowcount);
				result.setRowCountPerPage(num);
			}

			Integer start = (page-1) * num;
			Integer end = page * num;

			/**
			 * 获取结果集
			 */
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("object", inparam);
			param.put("page", page);
			param.put("page_begin", start);
			param.put("page_end", end);
			List<?> list = apsharepostDAO.findListByExample(param);

			result.setCurPageNum(page);
			result.setPageCount(pagecount);
			result.setResult((ArrayList<?>) list);
			result.setRowCount(rowcount);
			result.setRowCountPerPage(num);

		} catch (Exception e) {
			// logger.error(e);
			throw e;
		}

		return result;
	}

	@Override
	public Apsharepost getModelById(long id) {
		// TODO Auto-generated method stub
		return apsharepostDAO.selectByPrimaryKey((long)id);
	}

	@Override
	public int insertModel(Apsharepost record) {
		// TODO Auto-generated method stub
		int	ret=0;
		try{
			apsharepostDAO.insert(record);
			ret=1;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ret;
	}

	@Override
	public List<Apsharepost> listModel(ApsharepostExample example) {
		// TODO Auto-generated method stub
		return apsharepostDAO.selectByExample(example);
	}

	@Override
	public int updateModel(ApsharepostExample record) {
		// TODO Auto-generated method stub
		return apsharepostDAO.updateByPrimaryKeySelective(record);
	}

}
